package com.ow.dva.util.lockSocket;

import com.alibaba.fastjson.JSONObject;
import com.ow.dva.module.base.entity.json.RT;
import com.ow.dva.module.passingRecord.entity.PassingRecord;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.List;


public class SocketTask implements Runnable {


    private Socket socket;

    private String chipId;

    private String one;

    private String two;

    private String three;

    private String four;

    private String five;

    private boolean close = false; // 关闭连接标志位，true表示关闭，false表示连接


    public SocketTask(Socket socket) {
        this.socket = socket;
    }


    @Override
    public void run() {
        try {
            System.out.println("======>>>>等待客户端链接" );
            handleSocket();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 跟客户端Socket进行通信
     *
     * @throws Exception
     */
    private void handleSocket() {
//        while (true) {   //开始读取socket信息
            try {
                DataInputStream inputStream = null;  //数据输入流
                String strInputstream ="";

                inputStream =new DataInputStream(socket.getInputStream());
                ByteArrayOutputStream baos = new ByteArrayOutputStream();  // 可以捕获内存缓冲区的数据，转换成字节数组
                byte[] by = new byte[2048];
                int n;
                while((n=inputStream.read(by))!=-1){  //将流中的数据缓冲到字节数组中去，如果没到末尾返回的是真实个数，到末尾时，返回的是-1；
                    baos.write(by,0,n);    //readline将会把json格式破坏掉
                }
                strInputstream = new String(baos.toByteArray());
                JSONObject json = JSONObject.parseObject(strInputstream);
                String head = json.getString("head");

                System.out.println(json);

//                JsonParser jp = new JsonParser();
//                JsonObject jo = jp.parse(String.valueOf(json)).getAsJsonObject();
//                String head = jo.get("head").getAsString();
//                if (head.equals("passRec")){
//                    System.out.println("过车记录");
//
//                    String passing = jo.get("passingRecords").getAsString();
//
//                    HandleSocket.savePassRec(passing);
//                }else if(head.equals("park")){
//                    System.out.println("停车场信息");
//                    String passing = jo.get("parks").getAsString();
//
////                    HandleSocket.savePark(passing);
//                }else if(head.equals("parkinglot")){
//                    System.out.println("车道信息");
//                    String passing = jo.get("parkinglots").getAsString();
//
////                    HandleSocket.saveParkinglot(passing);
//                }

                socket.shutdownInput();
                baos.close();

            }catch (Exception e) {
                e.printStackTrace();
            }
//        }
    }


    /**
     * @return
     * @author: 姜延鹏
     * @Description: 获取过车记录
     * @date 2020/3/5
     */
    public RT getPassingRecord() {
        try {
            String serverMessage = "收到客户端呼叫";
            DataOutputStream ps = new DataOutputStream(socket.getOutputStream());
            ps.writeUTF(serverMessage);
            ps.flush();//一定要写，负责客户端会进入等待状态
            ps.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
       return RT.ok();
    }



    public RT savaPassingRecord(List<PassingRecord> passingRecords){

        return RT.ok();
    }


    public void setClose(boolean close) {
        System.out.println("/////");
        this.close = close;
    }

    //打印消息
    private void showMessage(String message) {
        System.out.println("客户端发来消息：" + message);
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public String getChipId() {
        return chipId;
    }

    public void setChipId(String chipId) {
        this.chipId = chipId;
    }

    public String getOne() {
        return one;
    }

    public void setOne(String one) {
        this.one = one;
    }

    public String getTwo() {
        return two;
    }

    public void setTwo(String two) {
        this.two = two;
    }

    public String getThree() {
        return three;
    }

    public void setThree(String three) {
        this.three = three;
    }

    public String getFour() {
        return four;
    }

    public void setFour(String four) {
        this.four = four;
    }

    public String getFive() {
        return five;
    }

    public void setFive(String five) {
        this.five = five;
    }


    public boolean clientConnected() {
        boolean flag = false;
        try {
            socket.sendUrgentData(0);
            flag = true;
        } catch (IOException e) {
//            System.out.println("客户端断开了连接。。。。。。。。。。。。。。。。。666");
            if (MySocketUtil.socketTaskMap.get("ps" + chipId) != null) {
                MySocketUtil.socketTaskMap.remove("ps" + chipId);
            }
        }
        return flag;
    }
}